package com.bms.cfcmms.dao;

import com.bms.cfcmms.model.system.module.Module;
import com.bms.cfcmms.util.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

/**
 * Created by carl on 2016/4/11.
 */
@Repository
public class ModuleDao extends BaseDao<Module> {

    private static final Logger log = LoggerFactory.getLogger(ModuleDao.class);

    public void attachDirty(Module module, String tableName) {
        updateEntity(module, module);
    }

    /**
     * 根据模块名称来查询模块信息.
     *
     * @param name 模块名称
     * @return 一个_Module对象
     */
    public Module queryForObjectByName(String name) {
        String sql = "select * from Module where moduleName=:moduleName";
        SqlParameterSource sqlParameterSource = new MapSqlParameterSource("moduleName", name);

        Object object;
        try {
            object = namedTemplate.queryForObject(sql, sqlParameterSource, new Module());
            return (Module) object;
        } catch (EmptyResultDataAccessException e) {
            log.warn(SpringUtils.getMessage("module.notfind", name), e);
            return null;
        }
    }
}
